define(['jquery','api','user','util'],function($,api,user,util){
	var vm,
		vmCache,
         wxid,
         timer,
         fromTo,
	   controller = {
	   init:function(fromto){
	   		fromTo=fromto;
	   		if(user.isLogin()){
		        window.location.hash="#/";
		        return;
		    }
	   	 wxid=api.getWxid();
		   api.headerShow(0);
		   api.footerShow(0);
		   api.setTitle('注册');	
		   
		   if(user.isRegister()){
		      util.msg(L('html_title_user_reg'));
		      setTimeout(function(){location.hash="#/"},600);
		      return;
		   }
		   
		   this.render();
	     	
	   },
	   render:function(){
	   	   var that=this;
	   	   $('#common').html('<div id="register"></div>');
	   	   
	       require(['text!pages/user/register.html','Vue'],function(tpl,Vue){
	       		var agent = user.getAgent(),
	       			pid = agent.agentpid,
	       			agentid = agent.agentid;

	            vm=new Vue({
	               el:'#register',
	               data:{
	               	  show:true,
	               	  imgSrc:false,
	               	  hasPid:pid?true:false,
	                  form:{
	                  	usertype:53, //经纪人
	                  	username:'',
		                captcha:'',
	                  	channelid:'',
						parentid:pid?pid:'', //1505
						iptparentid:'', //用户输入的机构号
		                mobile:'',
		                smcode:'',
		                loginpwd:'',
		                reloginpwd:'',
		                agentid:agentid?agentid:'', //推荐人(非必填)
		                checkbox:false, 
                        wxid:wxid
	                 }
	               },
	               methods:{
	                  changeCaptcha:function(){
	                      that.loadCaptcha();
	                  },
	                  getVerify:function(){
						    if(this.form.mobile!=''&&this.form.captcha!=''){
						    	util.Verify.send(this.form.mobile,this.form.captcha,$('#btnVerify'));
						    }else if(this.form.mobile==''){
						        util.msg('请先输入手机号！');
						    }else if(this.form.captcha==''){
						        util.msg('请先输入动态码！');
						    }
		              },
	                  register:function(){
		              		var data={},
		              		    $btn=$('#subBtn');
	                        for(var i in this.form){
	                           data[i]=this.form[i];
	                        }
	                        //用户输入了机构号就覆盖推荐的机构
	                        if(this.form.iptparentid){
		                        this.form.parentid = this.form.iptparentid; 
	                        }
                        	data.parentid=this.form.parentid;

		                	if(that.isValid(data)){

		                		$btn.val('提交中……').attr("disabled", "true");
		                		api.register(data,
		                			function(rs){
			                		    if(rs.retcode=='0'){
			                		    	util.msg('注册成功！');
			                		    	that.resetForm();
			                		    	user.clearAgent();// 注册好就清空推荐人数据
			                		    	user.setIsRegister(1);
			                		    	if(vmCache)vmCache={};
			                		    	setTimeout(function(){location.hash="#/";},1500);
			                		        
			                		    }else{
			                		    	$btn.val('立即注册').removeAttr("disabled");
			                		        util.dealErrorCode(rs.retcode);
			                		    }
			                		},
		                			function(){
		                				$btn.val('立即注册').removeAttr("disabled");
		                			}
		                		);
		                	}
		              }
	               },
	               template:tpl
	           });
	           
		       	if(fromTo && vmCache){
		   	 		vm.$data.form.username = vmCache.$data.form.username;
		   	 		vm.$data.form.mobile = vmCache.$data.form.mobile;
		   	 		vm.$data.form.smcode = vmCache.$data.form.smcode;
		   	 		vm.$data.form.captcha = vmCache.$data.form.captcha;
		   	 		vm.$data.form.loginpwd = vmCache.$data.form.loginpwd;
		   	 		vm.$data.form.reloginpwd = vmCache.$data.form.reloginpwd;
		   	 		vm.$data.form.parentid = vmCache.$data.form.parentid;
		   	 		vm.$data.form.iptparentid = vmCache.$data.form.iptparentid;
		   	 		vm.$data.form.checkbox = vmCache.$data.form.checkbox;
		   	 		vm.$data.imgSrc = vmCache.$data.imgSrc;
		   		}else{
			       // 实例化验证码
			       wxid && vm.changeCaptcha();
		   		}
		       
	       });
	   },
	   loadCaptcha:function(){
	       api.captcha(function(rs){
	           if(rs.retcode=='0'){
	               vm.imgSrc='data:image/png;base64,'+rs.imgcode;
	           }
	       })
	   },
	   isValid:function(data){
       	  var error=false,
       	      errorHash={
       	           username:'请输入姓名！',
       	           mobile:'请输入手机号！',
       	          smcode:'请输入验证码！',
       	          loginpwd:'请输登录密码！',
       	          reloginpwd:'请确认登录密码！',
       	          parentid:'请输入服务商编号！',
       	           password:'请输入登录密码！',
       	           checkbox:'请阅读并同意风险揭示书',
//     	           channelid:'缺少银行通道，请刷新后重试'
       	      };
       	  for(var key in errorHash){
       	  	  if (data[key]=='') {
		          error=errorHash[key];
		          break;
		      }
       	  }
       	  if(!error&&data.loginpwd!==data.reloginpwd){
       	     error="两次输入密码不一致！";
       	  }
		  if(error){
		  	  util.msg(error);
		      return false;
		  }else{
		      return true;
		  }
       },
       loadChannels:function(){
       	  var	that=this, 
       	  		temp=[],
   	  			cb=function(){
   	  				console.log(1);
   	  				if(timer)clearTimeout(timer);
   	  				that.loadChannels();
   	  			};
       	  api.channels(function(rs){
       	     if(rs.retcode=='0'){
       	         for(var i=0;i<rs.data.length;i++){
	   	       	  	   channel=rs.data[i];
	   	       	       temp.push(channel.channelid);
	   	       	  }
	   	       	 vm.form.channelid=temp.join(',');
       	     }else{
       	     	setTimeout(cb,2000);//取不到通道继续
       	     }
       	  })
       },
       resetForm:function(){ //清除表单
       		if(vm){
       			vm.form = {
                  	usertype:53, //经纪人
                  	username:'',
	                captcha:'',
                  	channelid:'',
					parentid:'', //382
	                mobile:'',
	                smcode:'',
	                loginpwd:'',
	                reloginpwd:'',
	                agentid:'', //推荐人(非必填)
	                checkbox:false,
                    wxid:wxid
                 };
       		}
       },
	   onRouteChange:function(){
			if(vm){
				vmCache=vm;
		       	vm.show=false;
       		}
	       api.footerShow(1);
	       if(timer)clearTimeout(timer);
	       if(fromTo)fromTo=null;
	   }
	};
    return controller;
})